using System;
using Spring.Data.NHibernate.Support;
using Spring.Data.NHibernate;
using NHibernate; 
using NHibernate.Criterion;
using System.Collections.Generic;
using System.Collections;
namespace pos.entity
{
    /// <summary>
    /// Summary description for 商品 数据库访问类
    /// </summary>
    public partial  class CommodityDao:Dao<Commodity>
    {

        public struct TCommodity
        {
           public string id;
           public string name;
           public double price;
           public double packAmount;
        }
        public ICriterion GeCriterions(String id)
        {
            IList list = this.GetAllCommodityList(id);
            ICriterion criterions = null;
            foreach (Commodity r in list)
            {
                if (criterions == null)
                    criterions = Restrictions.Eq("commodityID", r.commodityID);
                else
                    criterions = Restrictions.Or(criterions, Restrictions.Eq("commodityID", r.commodityID)); 
            }
            return criterions;
        }
        public IList GetAllCommodityList(String id)
		 {

             Commodity x=GetByCommodityID(id);
             
             string xid = id;
             if (x.packageCommodityID.Length>0)
                 xid=x.packageCommodityID;

             IList list = List(DC.Add(Restrictions.Or(Restrictions.Eq("commodityID", xid), Restrictions.Eq("packageCommodityID", xid))));
             return list;  
             
		 }
        public Hashtable GetAllCommodityTable(String id)
        {

            Commodity x = GetByCommodityID(id);

            string xid = id;
            if (x.packageCommodityID.Length > 0)
                xid = x.packageCommodityID;

            IList list = List(DC.Add(Restrictions.Or(Restrictions.Eq("commodityID", xid), Restrictions.Eq("packageCommodityID", xid))));
            
            Hashtable set = new Hashtable(list.Count);
            foreach (Commodity c in list)
            {
                TCommodity t=new TCommodity();
                t.id=c.commodityID;
                t.name=c.commodityName;
                t.packAmount=c.packageCommodityID.Length==0?1:c.packageAmount;
                t.price=c.retailPrice;
                set.Add(t.id, t);
                
            }
            return set;
        }
        public TCommodity[] GetCommodityInfo(String id)
        {

            Commodity x = GetByCommodityID(id);

            string xid = id;
            if (x.packageCommodityID.Length > 0)
                xid = x.packageCommodityID;

            IList list = List(DC.Add(Restrictions.Or(Restrictions.Eq("commodityID", xid), Restrictions.Eq("packageCommodityID", xid))));

            TCommodity[] ts = new TCommodity[list.Count];
            int i = 0;
            foreach (Commodity c in list)
            {
             
                ts[i].id = c.commodityID;
                ts[i].name = c.commodityName;
                ts[i].packAmount = c.packageCommodityID.Length == 0 ? 1 : c.packageAmount;
                ts[i].price = c.retailPrice;
                i++;

            }
            return ts;
        }
    }
}

